var http = require('http'), fs = require('fs'), url = require('url'),service = require('_service_realization');
// 创建服务器
http.createServer(function(request, response) {
	// 解析请求，包括文件名
	var pathname = url.parse(request.url).pathname,
		suffix = pathname.substring(pathname.lastIndexOf('.') + 1,pathname.length);
	console.log("Request for " + pathname + " received.");
	
	if(pathname == '/' || pathname == '/index.html' || pathname == '/index')
		pathname = '/view/app.html';
	
	// 从文件系统中读取请求的文件内容
	fs.readFile(pathname.substr(1), function(err, data) {
		if (err) {
			response.writeHead(404, {
				'Content-Type' : 'text/html'
			});
		} else {
			if(suffix == 'png' || suffix == 'jpg'){
				response.writeHead(200, {
					'Content-Type' : 'image/' + suffix,
					'Cache-Control' : 'no-cache',
					'Pragma':'No-cache'
				});
				response.write(data);
			}else if(suffix == 'css'){
				response.writeHead(200, {
					'Content-Type' : 'text/css',
					'Cache-Control' : 'no-cache',
					'Pragma':'No-cache'
				});
				response.write(data);
			}else if(suffix == 'woff' || suffix == 'eot' || suffix == 'woff2' || suffix == 'ttf' || suffix == 'svg'){
				response.writeHead(200);
				response.write(data);
			}else{
				response.writeHead(200, {
					'Content-Type' : 'text/html'
				});
				response.write(data.toString());
			}
		}
		response.end();
	});
}).listen(8086);
console.info('----------服务器已启动：8086-------');

//var io = require('socket.io').listen(8080);
//io.sockets.on('connection', function(socket) {
//	socket.on('query', function(message) {
//		console.info('server:query,message:' + message);
//		
//		message.result = service[message.method];
//		socket.emit('queryResult',message);
//		
//		console.info('server:query,end' + message);
//	});
//	socket.on('execute', function(message) {
//		console.info('server:execute,message:' + message);
//		
//		message.result = "========success===============";
//		socket.emit('executeResult',message);
//		
//		console.info('server:query,end' + message);
//	});
//});
//console.info('----------socket服务已启动：8080--------');
